//logs.js
const util = require('../../utils/util.js')

Page({
  data: {
    notfavorimg: 'http://img.gotohard.cn/img/icon/点赞未选中.png',
    favorimg: 'http://img.gotohard.cn/img/icon/点赞选中.png'
  },
  onReady: function () {
    var app = getApp();
    console.log(app)
    if (app.globalData.openid==null) {
      console.log('转到获取权限')
      this.getset()
    }
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    //获取所有文章
    wx.request({
      url: testUrl + '/article/findAll.do?userid=' + app.globalData.openid,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        //获取返回数据
        articleList = res.data;
        console.log(res)
        //讲图片json字符串转为json对象
        var app = getApp()
        for (var i = 0; i < articleList.length; i++) {
          if (articleList[i].articleImg!=''){
            articleList[i].articleImg = JSON.parse(articleList[i].articleImg);
          }
         
          articleList[i].createTime = app.dateto(articleList[i].createTime)
          // getUserInfo(i);
        }
        that.setData({
          articleList: res.data
        });
        console.log(that.data);
      }
    });
  },
  // 获取设置信息
  getset: function () {

    var user = { info: {} }
    // 获取用户信息
    wx.getSetting({
      success: function (res) {
        console.log("获取设置信息成功")
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              console.log("获取用户信息成功")
              // var that = getApp();
              console.log(res.userInfo)
              user.info.userinfo = res.userInfo
              // 登录
              wx.login({
                success: res => {
                  console.log(res)
                  // var that = getApp();
                  // that.code = res.code
                  user.code = res.code
                  var app = getApp()
                  var testurl = app.testUrl
                  wx.request({
                    url: testurl + '/user/getcode.do?code=',
                    data: {
                      code: res.code
                    },
                    header: {
                      'content-type': 'application/json' // 默认值
                    },
                    success: function (res) {
                      var app = getApp()
                      console.log(res)
                      user.info.openid = res.data.openid
                      user.info.userinfo.area = app.getc.getChina(user.info.userinfo.province) + " " + app.getc.getChina(user.info.userinfo.city),
                        console.log(user)
                      wx.setStorage({
                        key: "user",
                        data: user.info
                      })

                      // 设置用户信息
                      app.globalData.userInfo = user.info.userinfo
                      app.globalData.openid = user.info.openid
                      wx.request({
                        url: testurl + '/user/add.do',
                        method: "POST",
                        data: {
                          wxOpenid: user.info.openid,
                          wxUsername: user.info.userinfo.nickName,
                          userImg: user.info.userinfo.avatarUrl,
                          area: user.info.userinfo.area,
                          username: user.info.userinfo.nickName
                        },
                        header: {
                          'content-type': 'application/json' // 默认值
                        },
                        success: function (res) {
                          console.log(res)
                          var app = getApp()
                          console.log(app)
                        }
                      })

                    }
                  })
                  // 发送 res.code 到后台换取 openId, sessionKey, unionId
                },
                fail: function (res) {
                  console.log(res + "失败")
                }
              })


            },
            fail: function (e) {
              console.log(e + "获取用户信息失败")
            
            }
          })
        } else {
          console.log('没有获取授权')
          wx.redirectTo({
            url: '/pages/msg/msg_fail?page=2'
          })
        }
      },
      fail: function () {
        console.log("获取设置信息失败")
      }
    })


  },

  toDetail: function (event) {
    var articleId = event.currentTarget.dataset.id;//文章id
    wx.navigateTo({
      url: '/pages/article/article?id=' + articleId
    })
  },



  tonewArticle: function (event) {
    wx.navigateTo({
      url: '/pages/newArticle/newArticle'
    })
  },


  onLoad: function () {
    this.setData({
      
    })
  },
  onPullDownRefresh: function () {
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    this.onReady();
  },
  addfavor: function (e) {
    var articleId = e.currentTarget.dataset.id;//文章id
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    //获取所有文章
    wx.request({
      url: testUrl + '/article/addfavour.do',
      method: 'POST',
      data: {
        id: articleId,
        userid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if (res.data.success == true) {
          that.onReady()
        }
      }
    });

  




   
  },
  subfavor: function (e) {
    var articleId = e.currentTarget.dataset.id;//文章id
    var app = getApp();
    var testUrl = app.testUrl;
    var that = this;
    var articleList;
    //获取所有文章
    wx.request({
      url: testUrl + '/article/subfavour.do',
      method: 'POST',
      data: {
        id: articleId,
        userid: app.globalData.openid,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res);
        if (res.data.success == true) {
          that.onReady()
        }
      }
    });
  },

})
